<template>
  <div>
    <h3>医疗基本信息管理</h3>

    <!-- 查询功能 -->
    <el-input v-model="searchQuery" placeholder="请输入服务名称进行查询" style="width: 300px; margin-bottom: 20px;" clearable></el-input>
    <el-button type="primary" @click="fetchMedicalServices">查询</el-button>

    <!-- 添加医疗信息按钮 -->
    <el-button type="primary" @click="openDialog(null)">添加医疗信息</el-button>

    <!-- 医疗信息列表 -->
    <el-table :data="medicalInfoList" style="width: 100%">
      <el-table-column prop="infoid" label="信息ID" width="100"></el-table-column>
      <el-table-column prop="name" label="名称" width="180"></el-table-column>
      <el-table-column prop="type" label="类型" width="150"></el-table-column>
      <el-table-column prop="category" label="类别" width="150"></el-table-column>
      <el-table-column prop="cost" label="费用" width="150"></el-table-column>
      <el-table-column prop="parametervalue" label="参数值" width="150"></el-table-column>
      <el-table-column prop="description" label="描述" width="200"></el-table-column>
      <el-table-column prop="hospitallevel" label="适用医院等级" width="180"></el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteMedicalInfo(scope.row.infoid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑医疗信息对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible">
      <el-form :model="medicalInfoForm" label-width="120px">
        <el-form-item label="信息ID">
          <el-input v-model="medicalInfoForm.infoid" :disabled="isEditing"></el-input>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="medicalInfoForm.name"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="medicalInfoForm.type"></el-input>
        </el-form-item>
        <el-form-item label="类别">
          <el-input v-model="medicalInfoForm.category"></el-input>
        </el-form-item>
        <el-form-item label="费用">
          <el-input v-model="medicalInfoForm.cost"></el-input>
        </el-form-item>
        <el-form-item label="参数值">
          <el-input v-model="medicalInfoForm.parametervalue"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="medicalInfoForm.description"></el-input>
        </el-form-item>
        <el-form-item label="适用医院等级">
          <el-input v-model="medicalInfoForm.hospitallevel"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveMedicalInfo">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      medicalInfoList: [],
      medicalInfoForm: {
        infoid: '',
        name: '',
        type: '',
        category: '',
        cost: '',
        parametervalue: '',
        description: '',
        hospitallevel: ''
      },
      dialogVisible: false,
      dialogTitle: '',
      isEditing: false
    };
  },
  mounted() {
    this.fetchMedicalInfoList();
  },
  methods: {
    // 获取医疗基本信息列表
    fetchMedicalInfoList() {
      axios.get('http://localhost:8081/medicalBasicInfo/all')
          .then(response => {
            this.medicalInfoList = response.data
          })
          .catch(error => {
            console.error("Error fetching insured persons:", error);
          });
    },
    // 打开添加/编辑对话框
    openDialog(row) {
      if (row) {
        this.isEditing = true;
        this.dialogTitle = '编辑医疗基本信息';
        this.medicalInfoForm = {...row};
      } else {
        this.isEditing = false;
        this.dialogTitle = '添加医疗基本信息';
        this.resetForm();
      }
      this.dialogVisible = true;
    },
    // 重置表单
    resetForm() {
      this.medicalInfoForm = {
        infoid: '',
        name: '',
        type: '',
        category: '',
        cost: '',
        parametervalue: '',
        description: '',
        hospitallevel: ''
      };
    },
    // 保存医疗信息
    saveMedicalInfo() {
      if (this.isEditing) {
        axios.put(`http://localhost:8081/medicalBasicInfo/update`, this.medicalInfoForm)
            .then(() => {
              this.fetchMedicalInfoList();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error updating insured person:", error);
            });
      } else {
        axios.post('http://localhost:8081/medicalBasicInfo/add', this.medicalInfoForm)
            .then(() => {
              this.fetchMedicalInfoList();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error adding insured person:", error);
            });
      }
    },
    // 删除医疗信息
 // 删除医保机构
 deleteMedicalInfo(MedicalInfoid) {
      axios.delete(`http://localhost:8081/medicalBasicInfo/delete/${MedicalInfoid}`)
          .then(() => {
            this.fetchMedicalInfoList();
          })
          .catch(error => {
            console.error("Error deleting insured person:", error);
          });
    }
  }
};
</script>

<style scoped>
.el-button {
  margin-bottom: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
